<template>
    <van-sticky z-index="100">
        <van-nav-bar
            title="新增收货地址"
            left-text="返回"
            left-arrow
            @click-left="onBack"
        />
    </van-sticky>

    <van-address-edit
        :area-list="areaList"
        show-set-default
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
    />

</template>
<script setup>
import { getCurrentInstance, reactive } from 'vue';
import { areaList } from '@vant/area-data';

// 定义变量
const { proxy } = getCurrentInstance();
const business = reactive(proxy.$cookies.get('business') ? proxy.$cookies.get('business') : {});

// 提交事件
const onSave = async (info) => {

    // 组装数据
    let data = {
        busid:business.id,
        consignee:info.name,
        mobile:info.tel,
        code:info.areaCode,
        address:info.addressDetail,
        status:info.isDefault === true ? 1 : 0
    }

    // 发起请求
    let result = await proxy.$api.AddressAdd(data);

    if(result.code === 1)
    {
        proxy.$showNotify({
            type:'success',
            message:result.msg,
            duration:1500,
            onClose:() => {
                proxy.$router.go(-1);
            }
        });

        return;
    }else{
        proxy.$showNotify({
            type:'warning',
            message:result.msg,
            duration:1500
        });

        return;
    }
}

const onBack = () => {
    proxy.$router.go(-1);
    return;
}
</script>